package mbu;

import junit.framework.Assert;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Tests that StairShooter full gun rotation.
 * 
 * @author Michael Uranaka
 * 
 */
public class TestFullGunRotation extends RobotTestBed {

  // Global variables.
  String robotName = "";
  double startGunHeading;
  double endGunHeading;
  boolean gunFullySpins = false;
  int turnNumber = 1;

  /**
   * Specifies that SittingDuck and StairShootoer are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,mbu.StairShooter";
  }

  /**
   * This test runs for 5 round.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 5;
  }

  /**
   * After each turn, check if the current gun heading matches the last gun heading.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {

    // Select StairShooter from snapshot.
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];

    // For turn 1, get the gun heading at the end of the turn.
    if (turnNumber == 1) {
      robotName = robot.getName();
      double gunHeading = robot.getGunHeading();
      startGunHeading = gunHeading * (Math.PI / 180);
      turnNumber++;
    }
    else {
      // Compare the ending gun heading of the turn to the previous turn.
      // If the gun headings are the same then the gun did a full rotation.
      // Gun spins at the beginning of each turn.
      double gunHeading = robot.getGunHeading();
      endGunHeading = gunHeading * (Math.PI / 180);
      turnNumber++;

      if ((int)startGunHeading == (int)endGunHeading) {
        gunFullySpins = true;
      }
      startGunHeading = endGunHeading;
    }

  }

  /**
   * Returns a comma or space separated list like: x1,y1,heading1, x2,y2,heading2, which are the
   * coordinates and heading of robot #1 and #2. So "0,0,180, 50,80,270" means that robot #1 has
   * position (0,0) and heading 180, and robot #2 has position (50,80) and heading 270.
   * 
   * Override this method to explicitly specify the initial positions for your test cases.
   * 
   * Defaults to null, which means that the initial positions are determined randomly. Since battles
   * are deterministic by default, the initial positions are randomly chosen but will always be the
   * same each time you run the test case.
   * 
   * @return The list of initial positions.
   */
  @Override
  public String getInitialPositions() {
    return "0,0,0,200,200,0";
  }

  /**
   * After the battle, check if the gun was rotating completely.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    Assert.assertEquals("Check robot name", robotName, "mbu.StairShooter*");
    Assert.assertEquals("Check gun spin", gunFullySpins, true);
  }

}
